#include "CSoulConfig.h"

int CSoulBallConfigData::AddBallAttr(unsigned char ucBallLevel, CPlayerAttr& stPlayerAttr)
{
    if (ucBallLevel > MAX_SOUL_MAX_BALL_LEVEL)
    {
        CT_ERROR((""));
        return RET_SYS_ERROR;
    }

    //分级配置
    _astLevelAttr[ucBallLevel] = stPlayerAttr;

    //累计配置(必须从小到大添加配置)
    CPlayerAttr& stAttr = _astTotalAttr[ucBallLevel];
    stAttr.Clear();
    for (unsigned char i = 0; i <= ucBallLevel; ++i)
    {
        stAttr += _astLevelAttr[i];
    }
    
    return 0;
}

int CSoulBallConfigData::GetBallLevelAttr(unsigned char ucBallLevel, CPlayerAttr& stPlayerAttr)
{
    if (ucBallLevel > MAX_SOUL_MAX_BALL_LEVEL)
    {
        CT_ERROR((""));
        return RET_SYS_ERROR;
    }

    stPlayerAttr = _astLevelAttr[ucBallLevel];
    return 0;
}

int CSoulBallConfigData::GetBallTotalAttr(unsigned char ucBallLevel, CPlayerAttr& stPlayerAttr)
{
    if (ucBallLevel > MAX_SOUL_MAX_BALL_LEVEL)
    {
        CT_ERROR((""));
        return RET_SYS_ERROR;
    }

    stPlayerAttr = _astTotalAttr[ucBallLevel];
    return 0;
}

//////////////////////////////////////////////////////////////////////////

int CSoulBallConfig::AddConfig(CSoulBallConfigData& stConfigData)
{
    unsigned char ucBallID = stConfigData._ucBallID;
    if (ucBallID <= 0 || ucBallID > MAX_SOUL_MAX_BALL_COUNT)
    {
        CT_ERROR((""));
        return RET_SYS_ERROR;
    }

    if (_astBalls[ucBallID]._bHasConfig)
    {
        CT_ERROR(("duplicate config id %d", ucBallID));
        return RET_SYS_ERROR;
    }

    _astBalls[ucBallID] = stConfigData;
    _astBalls[ucBallID]._bHasConfig = true;

    return 0;
}

int CSoulBallConfig::GetConfig(unsigned char ucBallID, CSoulBallConfigData& stConfigData)
{
    if (ucBallID <= 0 || ucBallID > MAX_SOUL_MAX_BALL_COUNT)
    {
        CT_ERROR((""));
        return RET_SYS_ERROR;
    }

    if (!_astBalls[ucBallID]._bHasConfig)
    {
        CT_ERROR(("no config id %d", ucBallID));
        return RET_SYS_ERROR;
    }

    stConfigData = _astBalls[ucBallID];
    return 0;
}

//////////////////////////////////////////////////////////////////////////

int CSoulStatConfig::AddConfig(CSoulStatConfigData& stConfigData, CSoulBallConfig& stSoulBallConfig)
{
    int iRet = 0;
    unsigned char ucStatID = stConfigData._ucStatID;

    if (ucStatID > MAX_SOUL_STAT_LEVEL)
    {
        CT_ERROR((""));
        return RET_SYS_ERROR;
    }

    if (_astStatDatas[ucStatID]._bHasConfig)
    {
        CT_ERROR(("duplicate config id %d", ucStatID));
        return RET_SYS_ERROR;
    }

    _astStatDatas[ucStatID] = stConfigData;
    _astStatDatas[ucStatID]._bHasConfig = true;

    _ucMaxStatID = CT_MAX(_ucMaxStatID, stConfigData._ucStatID);

    //初始化累计属性
    for (int i = 0; i < stConfigData._astBallIDs.Size(); ++i)
    {
        unsigned char ucBallID = stConfigData._astBallIDs[i];
        CSoulBallConfigData stBallConfigData;
        iRet = stSoulBallConfig.GetConfig(ucBallID, stBallConfigData);
        CHECK_RETURN(iRet);

        CPlayerAttr stBallAttr;
        stBallConfigData.GetBallTotalAttr(MAX_SOUL_MAX_BALL_LEVEL, stBallAttr);
        _astTotalAttr[ucStatID] += stBallAttr;
    }

    return 0;
}

int CSoulStatConfig::GetConfig(unsigned char ucStatID, CSoulStatConfigData& stConfigData)
{
    if (ucStatID > MAX_SOUL_STAT_LEVEL)
    {
        CT_ERROR((""));
        return RET_SYS_ERROR;
    }

    if (!_astStatDatas[ucStatID]._bHasConfig)
    {
        CT_ERROR(("no config id %d", ucStatID));
        return RET_SYS_ERROR;
    }

    stConfigData = _astStatDatas[ucStatID];
    return 0;
}

int CSoulStatConfig::GetStatTotalAttr(unsigned ucStatID, CPlayerAttr& stAttr)
{
    if (ucStatID <= 0 || ucStatID > MAX_SOUL_STAT_LEVEL)
    {
        CT_WARNING(("invalid stat id %d", ucStatID));
        return RET_SYS_ERROR;
    }

    stAttr = _astTotalAttr[ucStatID];

    return 0;
}
